//
//  RBInvoiceHeaderAddVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/11.
//

#import "RBInvoiceHeaderAddVC.h"
#import "RBTextField.h"

@interface RBInvoiceHeaderAddVC ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) RBTextField *companyNameF;
@property (nonatomic, strong) RBTextField *companyTaxF;
@property (nonatomic, strong) RBTextField *regAddressF;
@property (nonatomic, strong) RBTextField *regPhoneF;
@property (nonatomic, strong) RBTextField *bankF;
@property (nonatomic, strong) RBTextField *bankAccountF;
@property(nonatomic, strong) UISwitch *defaultSwitch;
@property(nonatomic, assign) BOOL isDefault;

@end

@implementation RBInvoiceHeaderAddVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self fillData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"新增名头";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
//    [self.scrollView addView:({
//        UIView *view = UIView.new;
//        view.backgroundColor = UIColor.whiteColor;
//
//        UILabel *textL = [RanTool labelWithFontSize:Zoom(18) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:@"发票详情"];
//        [view addSubview:textL];
//        [textL mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(Zoom(19));
//            make.top.mas_equalTo(Zoom(30));
//        }];
//
//        view;
//    }) withHeight:Zoom(67) width:kRBMainScreenWidth top:Zoom(10) bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = [self cellWithTitle:@"公司名称" nes:YES placeHolder:@"请填写公司全称" sel:nil];
        self.companyNameF = (RBTextField *)[view viewWithTag:333];
        view;
    }) withHeight:Zoom(50) width:kRBMainScreenWidth top:Zoom(10) bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = [self cellWithTitle:@"公司税号" nes:YES placeHolder:@"请填写公司税号" sel:nil];
        self.companyTaxF = (RBTextField *)[view viewWithTag:333];
        view;
    }) withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = [self cellWithTitle:@"注册地址" nes:NO placeHolder:@"填写地址" sel:nil];
        self.regAddressF = (RBTextField *)[view viewWithTag:333];
        view;
    }) withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = [self cellWithTitle:@"注册电话" nes:NO placeHolder:@"填写电话" sel:nil];
        self.regPhoneF = (RBTextField *)[view viewWithTag:333];
        self.regPhoneF.type = RBTextFieldTypeNumber;
        view;
    }) withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = [self cellWithTitle:@"开户银行" nes:NO placeHolder:@"填写开户行" sel:nil];
        self.bankF = (RBTextField *)[view viewWithTag:333];
        view;
    }) withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = [self cellWithTitle:@"银行账号" nes:NO placeHolder:@"填写银行账号" sel:nil];
        self.bankAccountF = (RBTextField *)[view viewWithTag:333];
        self.bankAccountF.type = RBTextFieldTypeNumber;
        view;
    }) withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = [self cellWithTitle:@"默认发票" palceHolder:nil needArrow:NO];
        [view viewWithTag:3].userInteractionEnabled = NO;
        self.defaultSwitch = UISwitch.new;
        self.defaultSwitch.onTintColor = [UIColor colorWithHexString:@"#518CFF"];
        [self.defaultSwitch addTarget:self action:@selector(defaultSwitchValueChanged) forControlEvents:UIControlEventValueChanged];
        [view addSubview:self.defaultSwitch];
        [self.defaultSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-Zoom(18));
        }];
        view;
    }) withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UILabel *textL = [RanTool labelWithFontSize:Zoom(15) weight:1 color:UIColor.whiteColor align:1 text:@"立即提交"];
        textL.backgroundColor = [RBThemeConfig buttonBlueColor];
        [textL rbSetCornerRadius:Zoom(22)];
        [textL rbAddTapGesWithTarget:self action:@selector(confirmBtnTapped)];
        textL;
    }) withHeight:Zoom(44) width:Zoom(340) top:Zoom(70) bottom:Zoom(40) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
}


#pragma mark - Network

#pragma mark - Event

- (void)defaultSwitchValueChanged {
    self.isDefault = self.defaultSwitch.on;
}

- (void)confirmBtnTapped
{
    [self.view endEditing:YES];
    NSString *companyNameString = self.companyNameF.text;
    NSString *companyTaxString = self.companyTaxF.text;
    NSString *addressString = self.regAddressF.text;
    NSString *phoneString = self.regPhoneF.text;
    NSString *bankNameString = self.bankF.text;
    NSString *bankNumString = self.bankAccountF.text;
    
    if ([companyNameString isEqualToString:@""]) {
        [self.view showTip:self.companyNameF.placeholder completion:nil];
    } else if ([companyTaxString isEqualToString:@""]) {
        [self.view showTip:self.companyTaxF.placeholder completion:nil];
    } else {
        NSString *defaultString = self.isDefault ? @"1" : @"0";
        LCWeakSelf;
        [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"company_name"] = companyNameString;
            dicM[@"user_id"] = [RBAccountManager getUserId];
            dicM[@"company_shui"] = companyTaxString;
            dicM[@"is_default"] = defaultString;
            dicM[@"address"] = addressString;
            dicM[@"phone"] = phoneString;
            dicM[@"bank_name"] = bankNameString;
            dicM[@"bank_code"] = bankNumString;
        } path:_Invoice_add post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } failBlock:^(RBResponse *data) {
            [weakSelf.view showTip:data.message completion:nil];
        } reloadBlock:nil];
    }
}

#pragma mark - Setter

- (void)setIsDefault:(BOOL)isDefault
{
    if(_isDefault == isDefault) return;
    _isDefault = isDefault;
    self.defaultSwitch.on = isDefault;
}

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)cellWithTitle: (NSString *)title nes: (BOOL)nes placeHolder: (NSString *)placeHolder sel: (SEL)sel
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleL = UILabel.new;
    titleL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(14) weight:UIFontWeightMedium] color:[RBThemeConfig textBlackColor] string:title];
        if(nes)
        {
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(14) weight:UIFontWeightMedium] color:[RBThemeConfig priceRedColor] string:@"*"];
        }
    }];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(Zoom(19));
    }];
    
    RBTextField *field = RBTextField.new;
    field.placeholder = placeHolder;
    field.tag = 333;
    field.font = [UIFont systemFontOfSize:Zoom(14)];
    field.textColor = [RBThemeConfig textBlackColor];
    [view addSubview:field];
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(91));
        make.right.mas_equalTo(-Zoom(30));
        make.top.bottom.mas_equalTo(0);
    }];
    
    if (sel)
    {
        field.userInteractionEnabled = NO;
        [view rbAddTapGesWithTarget:self action:sel];
        
        UIImageView *imgView = UIImageView.new;
        imgView.image = [UIImage imageNamed:@"mine_arrow_right"];
        [view addSubview:imgView];
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(Zoom(-19));
            make.width.mas_equalTo(Zoom(7));
            make.height.mas_equalTo(Zoom(13));
        }];
        
    }
    
    return view;
}

- (UIView *)cellWithTitle: (NSString *)title
              palceHolder: (NSString *)placeHoler
                needArrow: (BOOL)needArrow
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleL = ({
        UILabel *view = UILabel.new;
        view.text = title;
        view.textColor = [RBThemeConfig textBlackColor];
        view.font = [UIFont systemFontOfSize:Zoom(14)];
        view;
    });
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(18));
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(78));
    }];
    
    UIImageView *arrowImgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"mine_arrow_right"]];
    arrowImgView.hidden = !needArrow;
    [view addSubview:arrowImgView];
    [arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Zoom(18));
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(7));
        make.height.mas_equalTo(Zoom(13));
    }];
    
    RBTextField *textF = RBTextField.new;
    textF.placeholder = placeHoler;
    textF.tag = 3;
    textF.textColor = [RBThemeConfig textBlackColor];
    textF.font = [UIFont systemFontOfSize:Zoom(14)];
    [view addSubview:textF];
    [textF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleL.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(arrowImgView.mas_left).offset(Zoom(-15));
    }];
    
    return view;
}

@end
